# ------------------------------------------------------------------------------
# Cargo Build Stage
# ------------------------------------------------------------------------------

FROM ekidd/rust-musl-builder:1.48.0 as cargo-build
USER root
RUN rustup default stable
RUN rustup target add x86_64-unknown-linux-musl

RUN mkdir -p /root/.cargo
COPY docker/.cargo/config /root/.cargo/
RUN export CARGO_HTTP_DEBUG=true
RUN export CARGO_LOG=cargo::ops::registry=debug
RUN mkdir -p /usr/src/actix_word_to_pdf
WORKDIR /usr/src/actix_word_to_pdf
COPY Cargo.toml Cargo.toml
RUN mkdir src/
RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build -v --release --target=x86_64-unknown-linux-musl
RUN rm -f target/x86_64-unknown-linux-musl/release/deps/actix_word_to_pdf*
COPY src ./src
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build -v --release --target=x86_64-unknown-linux-musl
# ------------------------------------------------------------------------------
# Final Stage
# ------------------------------------------------------------------------------

FROM alpine:latest
USER root
#install libreoffice
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN  apk add libreoffice \
        build-base \
        fontconfig && \
    fc-cache -f

RUN  apk add --no-cache build-base libffi libffi-dev
RUN  apk add --update ttf-dejavu
RUN  apk add openjdk8-jre


RUN mkdir -p /home/myapp/bin/
WORKDIR /home/myapp/bin/

COPY SimSun.ttf .
RUN cp SimSun.ttf /usr/share/fonts/
RUN chmod 644 /usr/share/fonts/SimSun.ttf
RUN fc-cache –fv

COPY --from=cargo-build /usr/src/actix_word_to_pdf/target/x86_64-unknown-linux-musl/release/actix_word_to_pdf .

RUN addgroup -g 1000 myapp
RUN adduser -D -s /bin/sh -u 1000 -G myapp myapp
RUN chown myapp:myapp actix_word_to_pdf
RUN chgrp -R myapp /tmp
RUN chmod -R g+w /tmp
#时区设置上海
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
USER myapp
EXPOSE 8080
CMD ["./actix_word_to_pdf"]